package com.whlilong.jxc.filemanager.editor;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;

import com.whlilong.jxc.db.DbManager;
import com.whlilong.jxc.dialog.GoodsInfoCreate;
import com.whlilong.jxc.dialog.GoodsInfoSearchByConditon;
import com.whlilong.jxc.editor.support.GoodsInfoEditorSupport;
import com.whlilong.jxc.listener.IEditorListener;
import com.whlilong.jxc.model.Goodsinfo;
import com.whlilong.jxc.print.IconSource;
import com.whlilong.jxc.print.PDocument;
import com.whlilong.jxc.print.PHLine;
import com.whlilong.jxc.print.PTableBoxProvider;
import com.whlilong.jxc.print.PTextBox;
import com.whlilong.jxc.print.PVSpace;
import com.whlilong.jxc.print.PrintPreview;
import com.whlilong.jxc.print.SWTPTable;
import com.whlilong.jxc.service.editor.IEditorService;
import com.whlilong.jxc.sys.CnToSpellFirstChar;
import com.whlilong.jxc.sys.ExtentionInfo;
import com.whlilong.jxc.sys.SetTableColColorListener;
import com.whlilong.jxc.treemodel.BrandInfoTreeChildren;
import com.whlilong.jxc.treemodel.BrandInfoTreeParent;
import com.whlilong.jxc.treemodel.BusinessInfoTreeChildren;
import com.whlilong.jxc.treemodel.BusinessInfoTreeParent;
import com.whlilong.jxc.treemodel.KeshangTreeChildren;
import com.whlilong.jxc.treemodel.KeshangTreeParent;
import com.whlilong.jxc.treemodel.MerchantTypeTreeChildren;
import com.whlilong.jxc.treemodel.MerchantTypeTreeParent;
import com.whlilong.jxc.ui.provider.BrandInfoTreeContentProvider;
import com.whlilong.jxc.ui.provider.BrandInfoTreeLabelProvider;
import com.whlilong.jxc.ui.provider.BusinessInfoTreeContentProvider;
import com.whlilong.jxc.ui.provider.BusinessInfoTreeLabelProvider;
import com.whlilong.jxc.ui.provider.DistrictTreeContentProvider;
import com.whlilong.jxc.ui.provider.DistrictTreeLabelProvider;
import com.whlilong.jxc.ui.provider.GoodsInfoContentProvider;
import com.whlilong.jxc.ui.provider.GoodsInfoLableProvider;
import com.whlilong.jxc.ui.provider.MerchantTypeTreeContentProvider;
import com.whlilong.jxc.ui.provider.MerchantTypeTreeLabelProvider;
import com.whlilong.jxc.ui.sorter.ClientInfoSorter;

/**
 * 商品信息编辑器
 * 
 * @author sjy
 * 
 */
public class GoodsInfoEditor extends AbstractEditor implements IEditorService,
		IEditorListener {

	private Shell shell;

	private Combo combo;

	private Label infoLabel;

	private Table table;

	private TableLayout tableLayout;

	private Button findBt;

	private Button add;

	private Label empty;

	protected List<Goodsinfo> list = new ArrayList<Goodsinfo>();

	private ExtentionInfo extentionInfo = null;

	private boolean isSave = true;

	private Label goodsidLabel;

	private Text goodsid;

	private Label goodsnameLabel;

	private Text goodsname;

	private String goodsNo;

	private String name;

	public GoodsInfoEditor() {
		// TODO Auto-generated constructor stub
	}

	protected Control createGroupText(Composite parent) {
		// TODO Auto-generated method stub
		Composite container = new Composite(parent, SWT.NONE);
		final GridLayout gl = new GridLayout(2, false);
		container.setLayout(gl);
		GridData gd = new GridData();
		gd.widthHint = 95;
		combo = new Combo(container, SWT.READ_ONLY);
		combo.add("1--商品区域");
		combo.add("2--商品类别");
		combo.add("3--商品品牌");
		combo.add("4--供应商");
		combo.select(0);
		combo.setLayoutData(gd);

		createTableViewer(container);
		addComboListener();
		createTreeViewer(container);

		return container;
	}

	private void createTableViewer(Composite parent) {
		// TODO Auto-generated method stub
		GridData gd = new GridData(GridData.FILL_BOTH);
		gd.verticalSpan = 2;
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout gl = new GridLayout(4, false);
		composite.setLayout(gl);
		composite.setLayoutData(gd);

		infoLabel = new Label(composite, SWT.NONE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.heightHint = 20;
		gd.horizontalSpan = 4;
		infoLabel.setLayoutData(gd);

		// 第二行

		gd = new GridData();
		gd.widthHint = 100;
		gd.heightHint = 20;
		goodsidLabel = new Label(composite, SWT.RIGHT);
		goodsidLabel.setText("商品编号：");
		goodsidLabel.setLayoutData(gd);
		goodsid = new Text(composite, SWT.BORDER);
		goodsid.setLayoutData(gd);

		goodsnameLabel = new Label(composite, SWT.RIGHT);
		goodsnameLabel.setText("商品名称：");
		goodsnameLabel.setLayoutData(gd);
		goodsname = new Text(composite, SWT.BORDER);
		goodsname.setLayoutData(gd);
		findBt = new Button(composite, SWT.NONE);
		findBt.setText("查询F7");

		findBt.setLayoutData(gd);
		findBt.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent e) {
				// TODO Auto-generated method stub
				search();
			}

		});

		tableViewer = new TableViewer(composite, SWT.BORDER | SWT.H_SCROLL
				| SWT.FULL_SELECTION);
		tableViewer.setUseHashlookup(true);

		gd = new GridData(GridData.FILL_BOTH);
		gd.horizontalSpan = 4;
		table = tableViewer.getTable();
		table.setLayoutData(gd);
		table.setLayout(tableLayout);
		table.setToolTipText("点击单元格对数据进行修改！");
		// 设置行高
		table.addListener(SWT.MeasureItem, new Listener() {
			public void handleEvent(Event event) {
				event.height = 20;
			}
		});

		GoodsInfoEditorSupport editingSupport = new GoodsInfoEditorSupport(
				tableViewer, 1, this);
		createColumn("ID", ClientInfoSorter.ID_ASC, ClientInfoSorter.ID_DESC,
				editingSupport);
		editingSupport = new GoodsInfoEditorSupport(tableViewer, 2, this);
		createColumn("商品编号", ClientInfoSorter.ID_ASC, ClientInfoSorter.ID_DESC,
				editingSupport);
		manager.addITextKeyChangeListener(editingSupport);

		editingSupport = new GoodsInfoEditorSupport(tableViewer, 3, this);
		createColumn("商品名称", ClientInfoSorter.ID_ASC, ClientInfoSorter.ID_DESC,
				editingSupport);
		manager.addITextKeyChangeListener(editingSupport);

		editingSupport = new GoodsInfoEditorSupport(tableViewer, 4, this);
		createColumn("拼音码", ClientInfoSorter.ID_ASC, ClientInfoSorter.ID_DESC,
				editingSupport);
		manager.addITextKeyChangeListener(editingSupport);

		editingSupport = new GoodsInfoEditorSupport(tableViewer, 5, this);
		createColumn("商品区域名称", null, null, editingSupport);
		manager.addITextKeyChangeListener(editingSupport);

		editingSupport = new GoodsInfoEditorSupport(tableViewer, 6, this);
		createColumn("商品类别名称", null, null, editingSupport);
		manager.addITextKeyChangeListener(editingSupport);

		editingSupport = new GoodsInfoEditorSupport(tableViewer, 7, this);
		createColumn("供应商名称", null, null, editingSupport);
		manager.addITextKeyChangeListener(editingSupport);

		editingSupport = new GoodsInfoEditorSupport(tableViewer, 8, this);
		createColumn("商品品牌", null, null, editingSupport);
		manager.addITextKeyChangeListener(editingSupport);

		editingSupport = new GoodsInfoEditorSupport(tableViewer, 9, this);
		createColumn("商品货号", null, null, editingSupport);
		manager.addITextKeyChangeListener(editingSupport);

		editingSupport = new GoodsInfoEditorSupport(tableViewer, 10, this);
		createColumn("参考进价", null, null, editingSupport);
		manager.addITextKeyChangeListener(editingSupport);

		editingSupport = new GoodsInfoEditorSupport(tableViewer, 11, this);
		createColumn("批发价", null, null, editingSupport);
		manager.addITextKeyChangeListener(editingSupport);

		editingSupport = new GoodsInfoEditorSupport(tableViewer, 12, this);
		createColumn("会员价", null, null, editingSupport);
		manager.addITextKeyChangeListener(editingSupport);

		editingSupport = new GoodsInfoEditorSupport(tableViewer, 13, this);
		createColumn("零售价", null, null, editingSupport);
		manager.addITextKeyChangeListener(editingSupport);

		editingSupport = new GoodsInfoEditorSupport(tableViewer, 14, this);
		createColumn("单位", null, null, editingSupport);
		manager.addITextKeyChangeListener(editingSupport);

		editingSupport = new GoodsInfoEditorSupport(tableViewer, 15, this);
		createColumn("规格", null, null, editingSupport);
		manager.addITextKeyChangeListener(editingSupport);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		initial();
		// initial the tabelViewer
		tableViewer.setContentProvider(new GoodsInfoContentProvider());
		tableViewer.setLabelProvider(new GoodsInfoLableProvider());

		tableViewer.setInput(list);

		table.addListener(SWT.EraseItem, new SetTableColColorListener(table));

		table.select(0);
		table.setFocus();

	}

	private void initial() {
		// TODO Auto-generated method stub
		infoLabel.setForeground(Display.getCurrent().getSystemColor(
				SWT.COLOR_BLUE));
		infoLabel.setText("添加，删除，查询相关数据。。。");
		list = getGoodsinfo(null, null);
	}

	@SuppressWarnings("unchecked")
	private List getGoodsinfo(String id, String name) {
		// TODO Auto-generated method stub
		List<Goodsinfo> newList = new ArrayList<Goodsinfo>();
		int[] idBuffer = { 0 };
		int[] idBuffer2 = { 0, 1 };
		Object[] idObject = { id };
		Object[] nameObject = { name };
		Object[] object = { id, name };
		if (id == null && name == null) {
			newList = (List) DbManager.getInstance().search(
					"com.whlilong.jxc.editor.getGoodsInfo");
		} else if (id != null && name == null) {
			newList = (List) DbManager.getInstance().search(
					"com.whlilong.jxc.editor.getGoodsInfo.getGoodsid",
					idBuffer, idObject);
		} else if (id == null && name != null) {
			newList = (List) DbManager.getInstance().search(
					"com.whlilong.jxc.editor.getGoodsInfo.getGoodsname",
					idBuffer, nameObject);
		} else {
			newList = (List) DbManager.getInstance().search(
					"com.whlilong.jxc.editor.getGoodsInfo.getGoods", idBuffer2,
					object);
		}
		return list = newList;

	}

	private void addComboListener() {
		// TODO Auto-generated method stub
		combo.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				int i = combo.getSelectionIndex();
				if (i == 0) {
					treeViewer.getTree().getColumn(0).setText("商品产地");
					treeViewer
							.setContentProvider(new DistrictTreeContentProvider());
					treeViewer
							.setLabelProvider(new DistrictTreeLabelProvider());
					treeViewer.setInput(extentionInfo.getInstance()
							.getKeshang());

					treeViewer.expandToLevel(2);
				}
				if (i == 1) {
					treeViewer.getTree().getColumn(0).setText("商品类别");
					treeViewer
							.setContentProvider(new MerchantTypeTreeContentProvider());
					treeViewer
							.setLabelProvider(new MerchantTypeTreeLabelProvider());
					treeViewer
							.setInput(extentionInfo.getInstance().getKstype());

					treeViewer.expandToLevel(2);
				}
				if (i == 2) {
					treeViewer.getTree().getColumn(0).setText("商品品牌");
					treeViewer
							.setContentProvider(new BrandInfoTreeContentProvider());
					treeViewer
							.setLabelProvider(new BrandInfoTreeLabelProvider());
					treeViewer.setInput(extentionInfo.getInstance()
							.getSpbrand());

					treeViewer.expandToLevel(2);
				}
				if (i == 3) {
					treeViewer.getTree().getColumn(0).setText("供应商");
					treeViewer
							.setContentProvider(new BusinessInfoTreeContentProvider());
					treeViewer
							.setLabelProvider(new BusinessInfoTreeLabelProvider());
					treeViewer.setInput(extentionInfo.getInstance()
							.getBusinessInfo());

					treeViewer.expandToLevel(2);
				}
			}

		});
	}

	private void createTreeViewer(Composite parent) {
		// TODO Auto-generated method stub
		treeViewer = new TreeViewer(parent, SWT.BORDER | SWT.V_SCROLL
				| SWT.FULL_SELECTION);
		treeViewer.setUseHashlookup(true);

		GridData gd = new GridData();
		gd.verticalAlignment = SWT.TOP;
		final Tree tree = treeViewer.getTree();
		tree.setLayoutData(new GridData(GridData.FILL_VERTICAL));
		final TreeColumn category = new TreeColumn(tree, SWT.LEFT);
		category.setWidth(100);
		category.setText("商品产地");
		treeViewer.setContentProvider(new DistrictTreeContentProvider());
		treeViewer.setLabelProvider(new DistrictTreeLabelProvider());
		treeViewer.setInput(extentionInfo.getInstance().getKeshang());

		tree.setHeaderVisible(true);
		tree.setLinesVisible(true);

		treeViewer.expandToLevel(2);

		treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {

			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				// TODO Auto-generated method stub
				IStructuredSelection selection = (IStructuredSelection) event
						.getSelection();
				if (selection.getFirstElement() instanceof KeshangTreeChildren) {
					KeshangTreeChildren ktc = (KeshangTreeChildren) selection
							.getFirstElement();
					String district = ktc.getKeshang().getKeshangarea();
					tableViewer.getTable().clearAll();
					tableViewer
							.setInput(sortDisplay(district, null, null, null));
				}
				if (selection.getFirstElement() instanceof KeshangTreeParent) {
					initial();
					treeViewer.getTree().setFocus();
					tableViewer.getTable().clearAll();
					tableViewer.setInput(list);
				}
				if (selection.getFirstElement() instanceof MerchantTypeTreeChildren) {
					MerchantTypeTreeChildren ktc = (MerchantTypeTreeChildren) selection
							.getFirstElement();
					String merchantType = ktc.getKstype().getKstype();
					tableViewer.getTable().clearAll();
					tableViewer.setInput(sortDisplay(null, merchantType, null,
							null));
				}
				if (selection.getFirstElement() instanceof MerchantTypeTreeParent) {
					initial();
					tableViewer.getTable().clearAll();
					tableViewer.setInput(list);
				}

				if (selection.getFirstElement() instanceof BusinessInfoTreeChildren) {
					BusinessInfoTreeChildren ktc = (BusinessInfoTreeChildren) selection
							.getFirstElement();
					String businessName = ktc.getHsinfo().getHsname();
					tableViewer.getTable().clearAll();
					tableViewer.setInput(sortDisplay(null, null, businessName,
							null));
				}
				if (selection.getFirstElement() instanceof BusinessInfoTreeParent) {
					initial();
					tableViewer.getTable().clearAll();
					tableViewer.setInput(list);
				}

				if (selection.getFirstElement() instanceof BrandInfoTreeChildren) {
					BrandInfoTreeChildren ktc = (BrandInfoTreeChildren) selection
							.getFirstElement();
					String brandName = ktc.getGoodsbrand().getGoodsbrandname();
					tableViewer.getTable().clearAll();
					tableViewer.setInput(sortDisplay(null, null, null,
							brandName));
				}
				if (selection.getFirstElement() instanceof BrandInfoTreeParent) {
					initial();
					tableViewer.getTable().clearAll();
					tableViewer.setInput(list);
				}

			}

		});
	}

	/**
	 * 分类显示条目 1-区域 2-类别 3-供应商 4-品牌
	 * 
	 */
	@SuppressWarnings("unchecked")
	protected List sortDisplay(String district, String merchantType,
			String businessName, String brandName) {
		List<Goodsinfo> newlist = new ArrayList<Goodsinfo>();
		int[] idBuffer = { 0 };
		Object[] districtObject = { district };
		Object[] typeObject = { merchantType };
		Object[] businessObject = { businessName };
		Object[] brandObject = { brandName };
		if (district != null) {
			newlist = (List) DbManager.getInstance().search(
					"com.whlilong.jxc.editor.goodsInfoEditor.getDistrict",
					idBuffer, districtObject);
		}
		if (merchantType != null) {
			newlist = (List) DbManager.getInstance().search(
					"com.whlilong.jxc.editor.goodsInfoEditor.getMerchantType",
					idBuffer, typeObject);
		}
		if (businessName != null) {
			newlist = (List) DbManager.getInstance().search(
					"com.whlilong.jxc.editor.goodsInfoEditor.getBusiness",
					idBuffer, businessObject);
		}
		if (brandName != null) {
			newlist = (List) DbManager.getInstance().search(
					"com.whlilong.jxc.editor.goodsInfoEditor.getBrand",
					idBuffer, brandObject);
		}
		return list = newlist;

	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

	@Override
	public void create() {
		Shell subshell = new Shell(shell, SWT.DIALOG_TRIM);
		GoodsInfoCreate sb = new GoodsInfoCreate(subshell, tableViewer);
		sb.open();
		if (!sb.isCacel()) {
			list = sb.getList();
			tableViewer.setInput(list);
			save();
			
		}
		else{
			list=(List)tableViewer.getInput();
			infoLabel.setText("提示：添加新数据后请点击保存来更新数据！");
		}
		
//		infoLabel.setText("提示：添加新数据后请点击保存来更新数据！");
		tableViewer.getTable().select(list.size() - 1);
		
		
	}

	@Override
	public void delete() {
		IStructuredSelection selection = (IStructuredSelection) tableViewer
				.getSelection();
		Goodsinfo goodsInfo = (Goodsinfo) selection.getFirstElement();
		if (!selection.isEmpty()) {
			if (MessageDialog.openConfirm(null, "提示", "你确定删除数据吗？")) {
				delete(goodsInfo);
			}
		} else {
			MessageDialog.openConfirm(null, "提示", "请从表格中选择一行数据进行删除！");
		}
	}

	private void delete(Goodsinfo goodsInfo) {
		// TODO Auto-generated method stub
		String id = goodsInfo.getGoodsid();
		if (!id.isEmpty()) {
			int[] idBuffer = { 0 };
			Object[] idObject = { id };
			List result = (List) DbManager.getInstance().search(
					"com.whlilong.jxc.editor.getGoodsInfo.getGoodsid", idBuffer, idObject);
			Goodsinfo goods = (Goodsinfo) result.get(0);
			DbManager.getInstance().delete(goods);
		}
		refresh();
	}

	@Override
	public void print() {
		// TODO Auto-generated method stub
		final PDocument doc = new PDocument("print");

		PTextBox t;

		t = new PTextBox(doc);

		t.setText("武汉立隆");

		new PVSpace(doc, 0.5);
		new PHLine(doc, 0.02, SWT.COLOR_RED);
		new PVSpace(doc, 0.5);

		// create the table
		SWTPTable swttable = new SWTPTable(doc);
		swttable.setTable(table);
		swttable.setBoxProvider(new PTableBoxProvider());
		PrintPreview pr = new PrintPreview(null, "信息打印", IconSource
				.getImage("print"), doc);
		pr.onPrint();

	}

	@Override
	public void refresh() {
		initial();
		combo.select(0);
		treeViewer.getTree().getColumn(0).setText("客户地区");
		treeViewer.setContentProvider(new DistrictTreeContentProvider());
		treeViewer.setLabelProvider(new DistrictTreeLabelProvider());
		treeViewer.setInput(extentionInfo.getInstance().getKeshang());
		treeViewer.expandToLevel(2);
		tableViewer.setInput(list);
	}

	@Override
	public void save() {
		if (!isSave) {
			MessageDialog.openWarning(null, "提示", "数据不能保存！");
			return;
		}

		manager.notifyListener();
		Iterator data = ((List) tableViewer.getInput()).iterator();
		if (!data.hasNext()) {
			MessageDialog.openWarning(null, "提示", "没有需要保存的数据！");
			return;
		}

		Job job = new Job("正在保存数据。。。") {

			@SuppressWarnings("unchecked")
			@Override
			protected IStatus run(IProgressMonitor monitor) {
				// TODO Auto-generated method stub
				monitor.beginTask("正在保存数据。。。", IProgressMonitor.UNKNOWN);
				for (int i = 0; i < list.size(); i++) {
					Goodsinfo goodInfo = (Goodsinfo) list.get(i);
					int id = goodInfo.getId();
					int[] idBuffer = { 0 };
					Object[] idObject = { id };
					List result = (List) DbManager.getInstance().search(
							"com.whlilong.jxc.editor.getGoodsInfoId", idBuffer,
							idObject);
					CnToSpellFirstChar c = new CnToSpellFirstChar();
					String pym = "";
					if (result.size() > 0) {
						String goodsName = goodInfo.getGoodsname();
						String goodsId = goodInfo.getGoodsid();
						if (goodsId != null && !goodsId.equals("")
								&& goodsName != null && !goodsName.equals("")) {
							Goodsinfo goods = (Goodsinfo) result.get(0);
							pym = c.makePy(goodsName);
							goods.setGoodsid(goodsId);
							goods.setGoodsname(goodsName);
							goods.setPym(pym.substring(0, pym.length() - 1));
							goods.setAddress(goodInfo.getAddress());
							goods.setGoodstpname(goodInfo.getGoodstpname());
							goods.setHsname(goodInfo.getHsname());
							goods.setGoodsbrandname(goodInfo
									.getGoodsbrandname());
							goods.setGoodshh(goodInfo.getGoodshh());
							goods.setCkjj(goodInfo.getCkjj());
							goods.setPfj(goodInfo.getPfj());
							goods.setHyj(goodInfo.getHyj());
							goods.setLsj(goodInfo.getLsj());
							goods.setUnit(goodInfo.getUnit());
							goods.setGg(goodInfo.getGg());
							monitor.worked(1);
							DbManager.getInstance().update(goods);
						} else {
							return Status.CANCEL_STATUS;
						}

					} else {
						String goodsName = goodInfo.getGoodsname();
						String goodsId = goodInfo.getGoodsid();
						if (goodsId != null && !goodsId.equals("")
								&& goodsName != null && !goodsName.equals("")) {
							Goodsinfo goods = new Goodsinfo();
							pym = c.makePy(goodsName);
							goods.setGoodsid(goodsId);
							goods.setGoodsname(goodsName);
							goods.setPym(pym.substring(0, pym.length() - 1));
							goods.setAddress(goodInfo.getAddress());
							goods.setGoodstpname(goodInfo.getGoodstpname());
							goods.setHsname(goodInfo.getHsname());
							goods.setGoodsbrandname(goodInfo
									.getGoodsbrandname());
							goods.setGoodshh(goodInfo.getGoodshh());
							goods.setCkjj(goodInfo.getCkjj());
							goods.setPfj(goodInfo.getPfj());
							goods.setHyj(goodInfo.getPfj());
							goods.setLsj(goodInfo.getLsj());
							goods.setUnit(goodInfo.getUnit());
							goods.setGg(goodInfo.getGg());
							DbManager.getInstance().insert(goods);
						} else {

							return Status.CANCEL_STATUS;
						}

					}
					list.get(i).setPym(pym.substring(0, pym.length() - 1));
				}
				monitor.done();
				return Status.OK_STATUS;
			}

		};
		job.addJobChangeListener(new JobChangeAdapter() {
			public void done(IJobChangeEvent event) {
				final boolean isSuccess = event.getResult().isOK();
				Display.getDefault().asyncExec(new Runnable() {
					public void run() {
						if (isSuccess) {

							MessageDialog.openInformation(null, "提示",
									"数据已经成功保存！");
							tableViewer.setInput(list);

						} else
							MessageDialog.openError(null, "错误", "编号或名称为空，数据保存失败！");
					}
				});
			}
		});
		job.setUser(false);
		job.setPriority(Job.SHORT);
		job.schedule();
	}

	@Override
	public void search() {
		goodsNo = goodsid.getText();
		name = goodsname.getText();
		find(goodsNo, name);

	}

	private void find(String id, String name) {
		// TODO Auto-generated method stub
		if ((id == null || id.equals("")) && (name == null || name.equals(""))) {
			tableViewer.setInput(getGoodsinfo(null, null));
		} else if ((id != null || !id.equals(""))
				&& (name == null || name.equals(""))) {
			tableViewer.setInput(getGoodsinfo(id, null));
		} else if ((id == null || id.equals(""))
				&& (name != null || !name.equals(""))) {
			tableViewer.setInput(getGoodsinfo(null, name));
		} else {
			tableViewer.setInput(getGoodsinfo(id, name));
		}
	}

	@Override
	public void searchByCondition() {
		Shell subshell = new Shell(shell, SWT.DIALOG_TRIM);
		GoodsInfoSearchByConditon sb = new GoodsInfoSearchByConditon(subshell);
		sb.open();
		if (!sb.isCacel()) {
			list = sb.getList();
			tableViewer.setInput(list);
		}
	}

	@Override
	public void addInfo() {
		// TODO Auto-generated method stub

	}

	@Override
	public List getList() {
		return this.list;
	}

	@Override
	public void noticeFault(String message) {
		infoLabel.setForeground(Display.getCurrent().getSystemColor(
				SWT.COLOR_RED));
		infoLabel.setText(message);
		this.isSave = false;

	}

	@Override
	public void noticeSuccess() {
		infoLabel.setText("");
		this.isSave = true;

	}

	@Override
	public void preprint() {
		// TODO Auto-generated method stub
		final PDocument doc = new PDocument("print");

		PTextBox t;

		t = new PTextBox(doc);

		t.setText("武汉立隆客商信息");

		new PVSpace(doc, 0.5);
		new PHLine(doc, 0.02, SWT.COLOR_RED);
		new PVSpace(doc, 0.5);

		// create the table
		SWTPTable swttable = new SWTPTable(doc);
		swttable.setTable(table);
		swttable.setBoxProvider(new PTableBoxProvider());
		
		PrintPreview pr = new PrintPreview(null, "信息打印", IconSource
				.getImage("print"), doc);
		pr.open();
	}

}
